package com.web.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 返回
 *
 * @author ekoo
 */
@Data
@Builder
@AllArgsConstructor
public class ApiResponse<T> implements Serializable {
    private static Integer SUCCESS = 900000;
    private static Integer FAIL = 999999;
    private int status;
    /**
     * 900000成功，非900000失败
     **/
    private String message;//错误具体内容
    protected T params;
    private long timestamp;

    public ApiResponse() {
        this.timestamp = System.currentTimeMillis();
    }

    public ApiResponse failure() {
        this.status = FAIL;
        this.message = "fail";
        this.timestamp = System.currentTimeMillis();
        return this;
    }

    public ApiResponse failure(Integer status, String message) {
        this.status = status;
        this.message = message;
        this.timestamp = System.currentTimeMillis();
        return this;
    }

    public ApiResponse failure(String message) {
        this.status = FAIL;
        this.message = message;
        this.timestamp = System.currentTimeMillis();
        return this;
    }

    public ApiResponse ok() {
        this.status = SUCCESS;
        this.message = "success";
        this.timestamp = System.currentTimeMillis();
        return this;
    }

    public ApiResponse ok(String message) {
        this.status = SUCCESS;
        this.message = message;
        this.timestamp = System.currentTimeMillis();
        return this;
    }
    @JsonIgnore
    public Boolean isOk(){
        if(status==SUCCESS)
            return true;
        else return false;
    }
    public static ApiResponse custom() {
        ApiResponse ApiResponse = new ApiResponse();
        return ApiResponse;
    }

    public static ApiResponse List() {
        ApiResponse<List> ApiResponse = new ApiResponse<List>();
        if (ApiResponse.getParams() == null) {
            ApiResponse.setParams(Lists.newArrayList());
        }
        return ApiResponse;
    }

    public static ApiResponse Map() {
        ApiResponse<Map> ApiResponse = new ApiResponse<Map>();
        if (ApiResponse.getParams() == null) {
            ApiResponse.setParams(Maps.newHashMap());
        }
        return ApiResponse;
    }

    public static ApiResponse Map(String key, Object value) {
        ApiResponse<Map> ApiResponse = new ApiResponse<Map>();
        if (ApiResponse.getParams() == null) {
            ApiResponse.setParams(Maps.newHashMap());
            ApiResponse.getParams().put(key, value);
        }
        return ApiResponse;
    }

    public static ApiResponse Map(Map map) {
        ApiResponse<Map> ApiResponse = new ApiResponse<Map>();
        if (ApiResponse.getParams() == null) {
            ApiResponse.setParams(Maps.newHashMap());
            ApiResponse.getParams().putAll(map);
        }
        return ApiResponse;
    }

    public ApiResponse addParams(T params) {
        this.params = params;
        return this;
    }


    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getMessage() {
        return StringUtils.defaultString(message);
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getParams() {
        return params;
    }

    public void setParams(T params) {
        this.params = params;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }
}